//
//  SignUpViewController.m
//  Kokozu
//
//  Created by Bismarrck on 11-7-17.
//  Copyright 2011年 Tsinghua University. All rights reserved.
//

#import "SignUpViewController.h"
#import "RegisterViewController.h"
#import "AccountViewController.h"
#import "UIViewController+BarItem.h"

@interface SignUpViewController ()

- (void)showRegisterView;
- (void)popBack;

@end

@implementation SignUpViewController

- (void)endEditing {
    [self resignFirstResponder];
}

@synthesize accountField;
@synthesize passwordField;
@synthesize delegate;
@synthesize mustSignUp;

- (id)initWithFrame:(CGRect)frame signUpEscape:(BOOL)escape
{
    self = [super init];
    if (self) {
        
        self.title = @"登陆";
        self.view.frame = frame;
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_light.png"]];
        self.mustSignUp = escape;
        
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    [accountField release];
    [passwordField release];
    
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Set the register bar item
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setFrame:CGRectMake(240, 7, 80, 30)];
    [aButton setTitle:@"注册" forState:UIControlStateNormal];
    [aButton.titleLabel setFont:[UIFont systemFontOfSize:14.0f]];
    [aButton setBackgroundImage:[UIImage imageNamed:@"nav_btn_downR.png"] forState:UIControlStateNormal];
    [aButton setBackgroundImage:[UIImage imageNamed:@"nav_btn_onR.png"] forState:UIControlStateSelected];
    [aButton addTarget:self action:@selector(showRegisterView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *registerBarItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
    self.navigationItem.rightBarButtonItem = registerBarItem;
    [registerBarItem release];
    
    // Set the cancel button
    [self createBarItemOn:kNavBarItemSideLeft frame:kNavBarLeftItemFrame title:@"返回" target:self action:@selector(pop)];
    
    // Set the title label
    UILabel *navBarTitle = [[UILabel alloc] init];
    [navBarTitle setFrame:CGRectMake(100, 0, 120, 44)];
    [navBarTitle setText:@"登录"];
    [navBarTitle setTextColor:[UIColor darkGrayColor]];
    [navBarTitle setTextAlignment:UITextAlignmentCenter];
    [navBarTitle setBackgroundColor:[UIColor clearColor]];
    [navBarTitle setFont:[UIFont boldSystemFontOfSize:20.0f]];
    
    [self.navigationItem setTitleView:navBarTitle];
    [navBarTitle release];
    [accountField addTarget:self action:@selector(endEditing) forControlEvents:UIControlEventEditingDidEndOnExit];
    [passwordField addTarget:self action:@selector(endEditing) forControlEvents:UIControlEventEditingDidEndOnExit];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    accountField = nil;
    passwordField = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark - Interface builder method

- (IBAction)signUpButtonPressed:(id)sender
{
    // This is the test code
    NSString *result = @"";
    
    NSLog(@"%@", USER_DEFAULT_NAME);
    NSLog(@"%@", USER_DEFAULT_PASS);
    
    if (USER_DEFAULT_NAME && USER_DEFAULT_PASS) {
        
        if ([USER_DEFAULT_NAME isEqualToString:[accountField text]] && [USER_DEFAULT_PASS isEqualToString:[passwordField text]]) {
            
            
            result = @"登录验证成功";
            [delegate newAccountDidSignUp];
            
            NSLog(@"sign up succeed!");
        }
        
    }
    else {
        result = @"登录验证失败";
    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"验证结果" 
                                                    message:result 
                                                   delegate:nil 
                                          cancelButtonTitle:@"好的" 
                                          otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
    
    [self popBack];
}

#pragma mark -
#pragma mark - Private method

- (void)showRegisterView
{
    RegisterViewController *viewController = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
}

- (void)popBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
